package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.FragmentUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.PictureRecordAdapter;
import flc.ast.adapter.TransferTabAdapter;
import flc.ast.bean.TransferTabBean;
import flc.ast.databinding.ActivityTransferRecordBinding;
import flc.ast.fragment.AppFragment;
import flc.ast.fragment.AppRecordFragment;
import flc.ast.fragment.AudioFragment;
import flc.ast.fragment.AudioRecordFragment;
import flc.ast.fragment.ContactsFragment;
import flc.ast.fragment.ContactsRecordFragment;
import flc.ast.fragment.PictureFragment;
import flc.ast.fragment.PictureRecordFragment;
import flc.ast.fragment.VideoFragment;
import flc.ast.fragment.VideoRecordFragment;
import stark.common.basic.event.EventStatProxy;

public class TransferRecordActivity extends BaseAc<ActivityTransferRecordBinding> {

    private TransferTabAdapter sendTabAdapter, receiveTabAdapter;
    private List<Fragment> mFragmentList;
    private int sendPosition = 0, receivePosition = 0;
    private boolean isSend = true;
    private boolean isEdit = false;


    @Override
    protected int onCreate() {
        return R.layout.activity_transfer_record;
    }

    @Override
    public void onBackPressed() {
        if (isEdit) {
            isEdit = false;
            if (isSend) {
                if (sendPosition == 0) {
                    PictureRecordFragment fragment = (PictureRecordFragment) mFragmentList.get(0);
                    fragment.setEdit(false);
                } else if (sendPosition == 1) {
                    VideoRecordFragment fragment = (VideoRecordFragment) mFragmentList.get(1);
                    fragment.setEdit(false);
                } else if (sendPosition == 2) {
                    AudioRecordFragment fragment = (AudioRecordFragment) mFragmentList.get(2);
                    fragment.setEdit(false);
                } else if (sendPosition == 3) {
                    AppRecordFragment fragment = (AppRecordFragment) mFragmentList.get(3);
                    fragment.setEdit(false);
                } else if (sendPosition == 4) {
                    ContactsRecordFragment fragment = (ContactsRecordFragment) mFragmentList.get(4);
                    fragment.setEdit(false);
                }
            } else {
                if (receivePosition == 0) {
                    PictureRecordFragment fragment = (PictureRecordFragment) mFragmentList.get(5);
                    fragment.setEdit(false);
                } else if (receivePosition == 1) {
                    VideoRecordFragment fragment = (VideoRecordFragment) mFragmentList.get(6);
                    fragment.setEdit(false);
                } else if (receivePosition == 2) {
                    AudioRecordFragment fragment = (AudioRecordFragment) mFragmentList.get(7);
                    fragment.setEdit(false);
                } else if (receivePosition == 3) {
                    AppRecordFragment fragment = (AppRecordFragment) mFragmentList.get(8);
                    fragment.setEdit(false);
                } else if (receivePosition == 4) {
                    ContactsRecordFragment fragment = (ContactsRecordFragment) mFragmentList.get(9);
                    fragment.setEdit(false);
                }
            }
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivTransferFileBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.tvSendRecord.setOnClickListener(this);
        mDataBinding.tvReceiveRecord.setOnClickListener(this);
        mDataBinding.ivTransferRecordEdit.setOnClickListener(this);

        mDataBinding.rvSendTab.setLayoutManager(new GridLayoutManager(mContext, 5));
        sendTabAdapter = new TransferTabAdapter();
        mDataBinding.rvSendTab.setAdapter(sendTabAdapter);
        sendTabAdapter.setOnItemClickListener(this);

        mDataBinding.rvReceiveTab.setLayoutManager(new GridLayoutManager(mContext, 5));
        receiveTabAdapter = new TransferTabAdapter();
        mDataBinding.rvReceiveTab.setAdapter(receiveTabAdapter);
        receiveTabAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvSendRecord://传送记录
                clearSelection();
                isSend = true;
                mDataBinding.tvSendRecord.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvSendRecord.setBackgroundResource(R.drawable.shape_record_sel);
                mDataBinding.rvSendTab.setVisibility(View.VISIBLE);
                FragmentUtils.showHide(sendPosition, mFragmentList);
                break;
            case R.id.tvReceiveRecord://接收记录
                clearSelection();
                isSend = false;
                mDataBinding.tvReceiveRecord.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvReceiveRecord.setBackgroundResource(R.drawable.shape_record_sel);
                mDataBinding.rvReceiveTab.setVisibility(View.VISIBLE);
                FragmentUtils.showHide(receivePosition, mFragmentList);
                break;
            case R.id.ivTransferRecordEdit://编辑
                isEdit = true;
                if (isSend) {
                    if (sendPosition == 0) {
                        PictureRecordFragment fragment = (PictureRecordFragment) mFragmentList.get(0);
                        fragment.setEdit(true);
                    } else if (sendPosition == 1) {
                        VideoRecordFragment fragment = (VideoRecordFragment) mFragmentList.get(1);
                        fragment.setEdit(true);
                    } else if (sendPosition == 2) {
                        AudioRecordFragment fragment = (AudioRecordFragment) mFragmentList.get(2);
                        fragment.setEdit(true);
                    } else if (sendPosition == 3) {
                        AppRecordFragment fragment = (AppRecordFragment) mFragmentList.get(3);
                        fragment.setEdit(true);
                    } else if (sendPosition == 4) {
                        ContactsRecordFragment fragment = (ContactsRecordFragment) mFragmentList.get(4);
                        fragment.setEdit(true);
                    }
                } else {
                    if (receivePosition == 0) {
                        PictureRecordFragment fragment = (PictureRecordFragment) mFragmentList.get(5);
                        fragment.setEdit(true);
                    } else if (receivePosition == 1) {
                        VideoRecordFragment fragment = (VideoRecordFragment) mFragmentList.get(6);
                        fragment.setEdit(true);
                    } else if (receivePosition == 2) {
                        AudioRecordFragment fragment = (AudioRecordFragment) mFragmentList.get(7);
                        fragment.setEdit(true);
                    } else if (receivePosition == 3) {
                        AppRecordFragment fragment = (AppRecordFragment) mFragmentList.get(8);
                        fragment.setEdit(true);
                    } else if (receivePosition == 4) {
                        ContactsRecordFragment fragment = (ContactsRecordFragment) mFragmentList.get(9);
                        fragment.setEdit(true);
                    }
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == sendTabAdapter) {
            sendTabAdapter.getItem(sendPosition).setSelected(false);
            sendTabAdapter.notifyItemChanged(sendPosition);
            sendPosition = position;
            sendTabAdapter.getItem(position).setSelected(true);
            sendTabAdapter.notifyItemChanged(position);
            FragmentUtils.showHide(position, mFragmentList);
        } else if (adapter == receiveTabAdapter) {
            receiveTabAdapter.getItem(receivePosition).setSelected(false);
            receiveTabAdapter.notifyItemChanged(receivePosition);
            receivePosition = position;
            receiveTabAdapter.getItem(position).setSelected(true);
            receiveTabAdapter.notifyItemChanged(position);
            FragmentUtils.showHide(position + 5, mFragmentList);
        }
    }

    @Override
    protected void initData() {
        String[] title = getResources().getStringArray(R.array.transfer_tab_title);
        List<TransferTabBean> listTab = new ArrayList<>();
        listTab.add(new TransferTabBean(title[0], true));
        listTab.add(new TransferTabBean(title[1], false));
        listTab.add(new TransferTabBean(title[2], false));
        listTab.add(new TransferTabBean(title[3], false));
        listTab.add(new TransferTabBean(title[4], false));
        sendTabAdapter.setList(listTab);
        receiveTabAdapter.setList(listTab);

        mFragmentList = new ArrayList<>();
        //传送记录
        mFragmentList.add(new PictureRecordFragment().newInstance(true));
        mFragmentList.add(new VideoRecordFragment().newInstance(true));
        mFragmentList.add(new AudioRecordFragment().newInstance(true));
        mFragmentList.add(new AppRecordFragment().newInstance(true));
        mFragmentList.add(new ContactsRecordFragment().newInstance(true));
        //接收记录
        mFragmentList.add(new PictureRecordFragment().newInstance(false));
        mFragmentList.add(new VideoRecordFragment().newInstance(false));
        mFragmentList.add(new AudioRecordFragment().newInstance(false));
        mFragmentList.add(new AppRecordFragment().newInstance(false));
        mFragmentList.add(new ContactsRecordFragment().newInstance(false));
        FragmentUtils.add(getSupportFragmentManager(), mFragmentList, R.id.flFragment, 0);
    }

    private void clearSelection() {
        mDataBinding.tvSendRecord.setTextColor(Color.parseColor("#80313131"));
        mDataBinding.tvSendRecord.setBackgroundResource(0);
        mDataBinding.tvReceiveRecord.setTextColor(Color.parseColor("#80313131"));
        mDataBinding.tvReceiveRecord.setBackgroundResource(0);
        mDataBinding.rvSendTab.setVisibility(View.GONE);
        mDataBinding.rvReceiveTab.setVisibility(View.GONE);
    }

}